package io.sk.controller;


import io.sk.common.BaseController;
import io.sk.common.base.BaseResult;
import io.sk.common.base.Constants;
import io.sk.common.utils.CheckDataUtils;
import io.sk.dto.LoginUserDto;
import io.sk.entity.PersonnelEntity;
import io.sk.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.jboss.logging.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 班级信息
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/5/8 18:04
 */
@RestController
@RequestMapping("")
@Api(tags = "注册登录接口")
public class LoginController extends BaseController {

    private Logger logger = Logger.getLogger(LoginController.class);

    @Autowired
    private SysUserService sysUserService;

    /**
     * @param personnelEntity 注册信息
     * @param type            人员类型：1、教师，2、家长：默认为老师
     * @param password        密码
     * @param request         HttpServletRequest
     * @return BaseResult
     */
    @PostMapping("/register")
    @ApiOperation("注册")
    public BaseResult register(
            PersonnelEntity personnelEntity,
            Integer type,
            String password,
            HttpServletRequest request
    ) {

        try {
            if (null == personnelEntity) {
                return sendResult400();
            }

            //人员类型：1、教师，2、家长
            if (Constants.USER_TEACHER != type && Constants.USER_PARENT != type) {
                type = Constants.USER_TEACHER;
            }
            if (StringUtils.isEmpty(password) || password.length() < Constants.INT_SIX || password.length() > Constants.INT_SIXTEEN) {
                return sendResult400("密码不能为空，且长度为6-16");
            }

            return sysUserService.register(personnelEntity, type, password);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return sendResult200();
    }

    /**
     * 登录
     *
     * @param phone    手机号
     * @param password 密码
     * @return BaseResult
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public BaseResult login(String phone, String password) {

        try {
            if (CheckDataUtils.isAnyEmptyString(phone, password)) {
                return sendResult431();
            }

            BaseResult baseResult = sysUserService.login(phone.trim(), password);
            if (Constants.RESPONSE_CODE_200 == baseResult.getCode()) {

                PersonnelEntity personnel = (PersonnelEntity) baseResult.getData();

                LoginUserDto userDto = new LoginUserDto();
                BeanUtils.copyProperties(personnel,userDto);
                baseResult.setData(userDto);

                setSession(Constants.SESSION, personnel);
            }

            return baseResult;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new BaseResult(Constants.RESPONSE_CODE_501, Constants.RESPONSE_MESSAGE_501);
        }
    }

}
